package com.qiujie.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qiujie.entity.AnnualLeave;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;


public interface AnnualLeaveMapper extends BaseMapper<AnnualLeave> {

    @Select("SELECT " +
            "id, staff_id AS staffId, year, total_days AS totalDays, used_days AS usedDays " +
            "FROM sys_annual_leave " +
            "WHERE staff_id = #{staffId} AND year = #{year}")
    AnnualLeave selectByStaffAndYear(@Param("staffId") Integer staffId, @Param("year") Integer year);

    @Update("UPDATE sys_annual_leave SET used_days = used_days + #{days} " +
            "WHERE staff_id = #{staffId} AND year = #{year} " +
            "AND (total_days - used_days) >= #{days}")
    int deductDaysSafely(@Param("staffId") Integer staffId,
                         @Param("year") Integer year,
                         @Param("days") Integer days);
}

